package com.gin.no_gay_alliance.adpter.emote;

import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.activity.EmotePreviewActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemCheckboxBinding;
import com.gin.no_gay_alliance.databinding.ItemEmoteCategoryBinding;
import com.gin.no_gay_alliance.manager.EmoteManager;
import com.gin.no_gay_alliance.utils.ViewUtils;
import com.gin.no_gay_alliance.viewmodel.emote.EmoteCategory;
import com.gin.no_gay_alliance.viewmodel.emote.EmoteGroupModel;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;

/**
 * 表情包分类适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/19 16:11
 **/
public class EmoteCategoryAdapter extends BaseBindingRecyclerViewAdapter<ItemEmoteCategoryBinding, EmoteCategory> implements CompoundButton.OnCheckedChangeListener, View.OnLongClickListener {
    private static final String TAG = EmoteCategoryAdapter.class.getSimpleName();

    public EmoteCategoryAdapter(@NonNull Context context, @NonNull List<EmoteCategory> data) {
        super(context, data);
    }

    @Override
    protected void onBindViewHolder(ViewHolder<ItemEmoteCategoryBinding> holder, @NonNull ItemEmoteCategoryBinding b, int position, EmoteCategory item) {
        HashSet<String> blockedNames = EmoteManager.getBLockedNames();

        TextView tvSource = b.tvSource;
        TextView tvVersion = b.tvVersion;
        RecyclerView rv = b.recyclerView;

        b.tvTitle.setText(item.name);
        tvSource.setText(Html.fromHtml(String.format(Locale.CHINA, "<a href='%s'>来源</>", item.source), Html.FROM_HTML_MODE_COMPACT));
        tvSource.setMovementMethod(LinkMovementMethod.getInstance());
        tvVersion.setText(String.format("v%s", item.version));
        ViewUtils.setGone(ObjectUtils.isEmpty(item.source), tvSource);
        ViewUtils.setGone(ObjectUtils.isEmpty(item.version), tvVersion);

        // 显示表情分组复选框
        rv.setAdapter(new BaseBindingRecyclerViewAdapter<ItemCheckboxBinding, EmoteGroupModel>(context, item.groups) {
            @Override
            protected void onBindViewHolder(ViewHolder<ItemCheckboxBinding> holder, @NonNull ItemCheckboxBinding b, int position, EmoteGroupModel group) {
                CheckBox cb = b.getRoot();
                cb.setText(group.name);
                cb.setChecked(!blockedNames.contains(group.name));
                cb.setOnCheckedChangeListener(EmoteCategoryAdapter.this);
                // 绑定tag
                cb.setTag(group);
                cb.setOnLongClickListener(EmoteCategoryAdapter.this);
            }

            @NonNull
            @Override
            protected ItemCheckboxBinding buildBinding(LayoutInflater inflater) {
                return ItemCheckboxBinding.inflate(inflater);
            }

            @Override
            protected boolean useZebraStripe() {
                return false;
            }
        });
    }

    @Override
    protected boolean useZebraStripe() {
        return false;
    }

    /**
     * 复选框状态变更时的回调
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        HashSet<String> blockedNames = EmoteManager.getBLockedNames();
        String text = buttonView.getText().toString();
        //修改屏蔽名单
        if (isChecked) {
            Log.d(TAG, "选中分组: " + text);
            blockedNames.remove(text);
        } else {
            Log.d(TAG, "屏蔽分组: " + text);
            blockedNames.add(text);
        }
        EmoteManager.setBLockedNames(blockedNames);
    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getTag() instanceof EmoteGroupModel) {
            Log.d(TAG, "onLongClick: " + "预览表情分组内容");
            EmotePreviewActivity.startActivity(context, (EmoteGroupModel) v.getTag());
            return true;
        }
        return false;
    }
}
